<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use App\Http\Model\Type;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CategoryController extends CommonController
{
    //get  admin/category   全部分类列表
    public function index(){
        $cate_datas = Category::orderBy('sort','asc')->get();
        $cate_datas_tree = (new Category)->getTree($cate_datas);
        return view('admin.category.index',compact('cate_datas_tree'));
    }



    //post  admin/category  获取要添加的数据并存储到数据库
    public function store(){
        $input = Input::except('_token');
        //验证
        $rules = [
            'cname'=>'required',

        ];
        $messages = [
            'cname.required'=>'分类名称不能为空'
        ];

        $validator = Validator::make($input,$rules,$messages);
        if($validator->passes()){
            $re = Category::create($input);
            if($re){
                return redirect('admin/category');
            }else{
                return back()->with('errors','数据填入失败');
            }
            
        }else{
            return back()->getErrors($validator);
        }


        
    }

    //get. admin/category/create   添加页面的显示及数据分配
    public function create(){
        //无限极分类
        $data = Category::all();
        $cate = new Category();
        $arr = $cate->getTree($data);
        
        //类型
        $type_datas = Type::all();
        
        return view('admin.category.add',compact('arr','type_datas'));
    }


    //get. admin/category/create_son/{cid}/{tid}
    public function create_son($cid,$tid){
        
        $type_data = Type::find($tid);
        $cate_data = Category::find($cid);

        return view('admin.category.add_son',compact('cate_data','type_data'));
    }


    //get admin/category/{category}
    public function show(){

    }

    //delete admin/category/{category}
    public function destroy($cid){
        $pid = Category::where('cid',$cid)->pluck('pid');
//        dd($pid);
        Category::where('pid',$cid)->update(['pid'=>$pid[0]]);
        $re = Category::where('cid',$cid)->delete();
        if($re){
            $data = [
                'status'=>1,
                'msg'=>"分类删除成功"
            ];
        }else{
            $data = [
                'status'=>0,
                'msg'=>"分类删除失败"
            ];
        }

        return $data;
    }

    //put admin/category/{category}
    public function update($cid){
        $input = Input::except('_token','_method');
        $re = Category::where('cid',$cid)->update($input);
        if($re){
            return redirect('admin/category');
        }else{
            return back()->with('msg','分类更新失败');
        }
    }

    //get admin/category/{category}/edit
    public function edit($cid){
        $cate_model = new Category();
        //分配当前数据
        $cate_data = Category::find($cid)->with('cate_type')->where('cid',$cid)->first();
        $cate_cur_pid = $cate_data->pid;
        //分配当前分类所在的顶级分类的所有子分类
        $cate_all = Category::all();

        $cate_cids = $cate_model->parents_sons($cate_all, $cate_cur_pid);
//        dd($cate_cids);
        $field_datas = Category::whereIn('cid',$cate_cids)->get();

        $cate_datas_tree = $cate_model->getTree($field_datas);
//        dd($cate_datas_tree);
        //分配所有类型
        $type_all = Type::all();
        return view('admin.category.edit',compact('cate_data','cate_datas_tree','type_all'));
    }




    //排序改变
    public function order_change()
    {
        $input = Input::except('_token');

        $cate_sort_data = Category::find($input['cid']);
        $cate_sort_data->sort=$input['sort'];
        $re = $cate_sort_data->update();
        if($re){
            $data = [
                'status'=>1,
                'msg'=>'排序修改成功'
            ];
        }else{
            $data = [
                'atatus'=>0,
                'msg'=>'排序修改失败'
            ];
        }
        return $data;
    }
}
